<?php
class Control_DataAnalysis_tutorialStep {

	function tutorialStep() {
		$nowGame = $game 		= 	MooForm::request('game');
		$platform 	= 	MooForm::request('platform');
		$channel	= 	MooForm::request('channel');
		$server 	= MooForm::request('server');
		$startDate 	= 	MooForm::request('startTime');
		$endDate 	= 	MooForm::request('endTime');
		
		$game = MooObj::get('Game')->transportGame($game);
		$nowChannel = $channel = MooObj::get('Game')->transportChannel($game, $channel);
		
		if(!$startDate) {
			$startDate 	= 	date('Y-m-d', time());
		}
		if(!$endDate) {
			$endDate 	= 	date('Y-m-d', time());
		}
		$action 		= 	MooForm::request('action');
		MooView::set('action', $action);
		
		MooView::set('startDate', $startDate);
		MooView::set('endDate', $endDate);
		if($action == 0) {
			MooView::render('tutorialStep');
		} else if($action == 1) {
			MooObj::get('Control_Data')->setPlatformData($game);
			$urlConf = MooConfig::get('main.dataAnalysis.fetch_tutorial_step');
			$url = $urlConf . "?game=" . $game . "&beginDate=" . $startDate . "&endDate=" . $endDate;
			if ($platform) {
				$url = $url . "&platform=" . $platform;
			}
			if ($channel) {
				$url = $url . "&channel=" . $channel;
			}
            if($server) {
                MooView::set('server', $server);
                $url = $url . "&server=" . $server; //入参名???
            }
           //var_dump($url);exit;
			// $url = "http://182.254.230.39:9001/fetch_gold?game=wow&changetype=1&beginDate=2015-02-28&endDate=2015-03-31&platform=appstore&channel=appstore";
            $servers = MooObj::get('Gm')->getServerListByGame($game);
            MooView::set('servers', $servers);

			$data = MooUtil::curl_send($url);
			$dataArr = MooJson::decode($data);
			
			$actionConf = MooConfig::get('action_name.step');
			$totalGolds = 0;
			$goldRates = array();
			if($dataArr) {
				foreach ($dataArr as $key => $val) {
					$totalGolds += $val['count'];//上报的玩家的数量
				}
								
				foreach ($dataArr as $k => $v) {
				    $dataArr[$k]['step'] =  $actionConf[$v['step']];
					$dataArr[$k]['rateNum'] = MooUtil::getPercent($v['count'], $totalGolds, true); // 加true 不要百分号
					$dataArr[$k]['rate'] = MooUtil::getPercent($v['count'], $totalGolds);//百分比

				}
				foreach ($dataArr as $k => $v) {
					//$goldRates[$v['changeaction']] = $v['rateNum'];
					$goldRates[$k][0]= "step". $v['step'];
					$goldRates[$k][1]= $v['rateNum'];
				}
			}
			
			$viewData = MooJson::encode($goldRates);
			// 格式化数据
			$viewData = str_replace(',"',',',$viewData);
			$viewData = str_replace('"]',']',$viewData);
			//导出excel
			$type = MooForm::request('type');
			if($goldRates && $type == "daochu") {
				$excelData = array();
				
				$sDate = date("Ymd", strtotime($beginDate));
				$eDate = date("Ymd", strtotime($endDate));
				$name = $game. "_" .  $sDate . "_" . $eDate;				
				
				$excelRs = array(
					0 => array(
						0 => '',
						1 => $name,
					),
					1 => array(
						0 => '步骤',
						1 => '数量',
						2 => '百分比',
				));
				
/* 				var_dump($dataArr);
				exit; */
				
				foreach ($dataArr as $key=>$val) {
					$arr[] = $val['step'];					
					$arr[] = $val['count'];
					$arr[] = $val['rate'];
					$excelData[] = $arr;
					$arr = array();
				}
				
				$excelData = array_merge($excelRs, $excelData);
				$writeExcelArr[0]['name'] = $name;
				$writeExcelArr[0]['data'] = $excelData;
					
				$excelPath = ROOT_PATH . "/export/orders/" . $name . ".xls";
				$rs = MooObj::get('Control_ExcelConfMaker_Excel')->write($excelPath, $writeExcelArr);
				$this->downFile($excelPath, $name . ".xls");
				exit;
			}
			
			//
			if($platform) {
				MooView::set('platform', $platform);
			}
			if ($channel) {
				MooView::set('channel', $nowChannel);
			}
			MooView::set('nowGame', $nowGame);
			
			MooView::set('viewData', $viewData);
			MooView::set('goldSource', $dataArr);
			MooView::render('tutorialStep');
		} else {
			exit("error!");
		}
	}
	
	// 下载文件
	private function downFile($filepath, $fileName) {
		$fp			=	fopen($filepath,"r");
		$file_size	=	filesize($filepath);
		//下载文件需要用到的头
		header("Content-type: application/octet-stream");
		header("Accept-Ranges: bytes");
		header("Accept-Length:".$file_size);
		header("Content-Disposition: attachment; filename=".$fileName);
		$buffer		=	1024;
		$file_count	=	0;
		//向浏览器返回数据
		while(!feof($fp) && $file_count < $file_size){
			$file_con	=	fread($fp,$buffer);
			$file_count	+=	$buffer;
			echo $file_con;
		}
		fclose($fp);
	}
}